package com.snbc.Io;

import java.io.*;

/**
 * @Classname BufferRead
 * @Description TODO
 * @Date 2019/9/27 21:37
 * @Created by Acer
 */
public class BufferRead {

    public static void main(String[] args) {
        File file = new File("F:\\123444mmm\\123.txt");
        // 创建文件字符流 缓存字符输入流 BufferedReader 可以一次读取一行数据
        // 缓存流必须建立在一个存在的流的基础上
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);

            while (true) {// 一次读一行
                String line = br.readLine();
                if (line == null) {
                    break;
                }
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //PrintWriter 缓存字符输出流， 可以一次写出一行数据
        File file1 = new File("F:\\123444mmm\\qwqe.txt");
        FileWriter fw = null;
        PrintWriter pw = null;
        try {
            fw = new FileWriter(file1, true);//第二个参数为追加写入
            pw = new PrintWriter(fw);
            pw.println("garen kill teemo");
            pw.println("teemo revive after 1 minutes");
            pw.println("teemo try to garen, but killed again111");
            System.out.println("aa");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
